package com.onehouse.domain.room.service;

import com.onehouse.domain.room.model.RoomTypeEntity;
import com.onehouse.domain.room.repository.IRoomRepository;
import com.onehouse.domain.room.repository.IRoomTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoomTypeService implements IRoomTypeService {
    @Autowired
    private IRoomTypeRepository roomTypeRepository;

    @Autowired
    private IRoomRepository roomRepository;

    public List<RoomTypeEntity> listAllRoomType() {
        return roomTypeRepository.selectList();
    }

    public void createRoomType(RoomTypeEntity roomTypeEntity) {
        if (roomTypeRepository.existedByTypeName(roomTypeEntity.getTypeName())) {
            throw new IllegalArgumentException("Room type name already exists");
        }
        roomTypeRepository.insert(roomTypeEntity);
    }

    public void updateRoomType(RoomTypeEntity roomTypeEntity) {
        if (!roomTypeRepository.selectById(roomTypeEntity.getTypeId()).isPresent()) {
            throw new IllegalArgumentException("Room type not found");
        }
        roomTypeRepository.updateById(roomTypeEntity);
    }

    public void removeRoomType(Long typeId) {
        // 检查是否有房间关联该类别
        if (roomRepository.existedById(typeId)) {
            throw new IllegalStateException("Cannot delete room type with associated rooms");
        }
        roomTypeRepository.deleteById(typeId);
    }
}
